﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;
using com.fukefu.sql;

namespace com.fukefu
{
    //委托
    public delegate void MyDelegate0();
    public delegate void MyDelegate1(string value);
    public delegate void MyDelegate1_1(int value);
    public delegate void MyDelegate1_2(object value);
    public delegate object MyDelegate2(string name, ControlValue key);
    public delegate void MyDelegate2_1(string name, object key);
    public delegate void MyDelegate3(string name, ControlValue key, object value);

    public class BaseForm : Form
    {
        private bool scroll = true;
        //查找一个性对的控件
        protected Control GetControl(object obj)
        {
            return GetControl(obj.ToString());
        }
        protected Control GetControl(string name)
        {
            if (!string.IsNullOrEmpty(name))
            {
                Control[] ctr = this.Controls.Find(name, true);
                return ctr.Length > 0 ? ctr[0] : null;
            }
            return null;
        }
        //获取属性值
        protected object GetControl(string name, ControlValue key)
        {
            try
            {
                Control c = GetControl(name);
                if (c != null && c.InvokeRequired)
                {
                    IAsyncResult asy = c.BeginInvoke(new MyDelegate2(GetControl), name, key);
                    return c.EndInvoke(asy);
                }
                else if (c != null)
                {
                    switch (key)
                    {
                        case ControlValue.Text:
                            {
                                return c.Text;
                            }
                        case ControlValue.Name:
                            {
                                return c.Name;
                            }
                        case ControlValue.Enable:
                            {
                                return c.Enabled;
                            }
                        case ControlValue.Checked:
                            {
                                return ((CheckBox)c).Checked;
                            }
                        case ControlValue.DateTimePicker:
                            {
                                return ((DateTimePicker)c).Value.ToShortDateString();
                            }
                    }
                }
            }
            catch (Exception e)
            {
                ShowMessageDialog("程序错误:" + e.Message + "(BaseForm)");
            }
            return null;
        }
        //设置属性值
        protected void SetControl(string name, ControlValue key, object value)
        {
            try
            {
                Control c = GetControl(name);
                if (c != null && c.InvokeRequired)
                {
                    c.BeginInvoke(new MyDelegate3(SetControl), name, key, value);
                }
                else if (c != null)
                {
                    switch (key)
                    {
                        case ControlValue.WebBrowser:
                            {
                                WebBrowser web = c as WebBrowser;
                                web.DocumentText = value.ToString();
                                break;
                            }
                        case ControlValue.Text:
                            {
                                c.Text = value.ToString();
                                break;
                            }
                        case ControlValue.Name:
                            {
                                c.Name = value.ToString();
                                break;
                            }
                        case ControlValue.Enable:
                            {
                                c.Enabled = Convert.ToBoolean(value);
                                break;
                            }
                        case ControlValue.ListBox:
                            {
                                ListBox lb = (ListBox)c;
                                lb.BeginUpdate();
                                lb.Items.Add(value.ToString());
                                if (scroll)
                                {
                                    lb.SelectedIndex = lb.Items.Count - 1;
                                }
                                lb.EndUpdate();
                                break;
                            }
                        case ControlValue.Combobox:
                            {

                                ((ComboBox)c).Items.Add(value);
                                break;
                            }
                        case ControlValue.StripComboBox:
                            {
                                //c.Items.Add(value);
                                break;
                            }
                        case ControlValue.PictureBox:
                            {
                                ((PictureBox)c).Image = value as Image;
                                break;
                            }
                        case ControlValue.ListView:
                            {
                                ListView lv = c as ListView;
                                lv.BeginUpdate();
                                lv.Items.Clear();
                                List<ListViewItem> list = value as List<ListViewItem>;
                                foreach (ListViewItem lvt in list)
                                {
                                    lv.Items.Add(lvt);
                                }
                                lv.EndUpdate();
                                break;
                            }
                        case ControlValue.ListViewHeader:
                            {
                                ListView lv = c as ListView;
                                lv.BeginUpdate();
                                lv.Items.Clear();
                                lv.Columns.Clear();
                                lv.Columns.AddRange(value as ColumnHeader[]);
                                int width = 80;
                                //设置列宽
                                for (int i = 0; i < lv.Columns.Count; i++)
                                {
                                    width = 80;
                                    switch (lv.Columns[i].Text)
                                    {
                                        case "域名":
                                            {
                                                width = 130;
                                                break;
                                            }
                                        case "名称":
                                            {
                                                width = 180;
                                                break;
                                            }
                                        case "网址":
                                            {
                                                width = 420;
                                                break;
                                            }
                                        case "关键字":
                                            {
                                                width = 200;
                                                break;
                                            }
                                        case "日期":
                                            {
                                                break;
                                            }
                                        case "时间":
                                            {
                                                width = 120;
                                                break;
                                            }
                                        case "描述":
                                            {
                                                width = 350;
                                                break;
                                            }
                                        case "数量":
                                            {
                                                break;
                                            }
                                    }
                                    lv.Columns[i].Width = width;
                                }
                                lv.EndUpdate();
                                break;
                            }
                    }
                }
            }
            catch (Exception e)
            {
                ShowMessageDialog("程序错误:" + e.Message + "(BaseForm)");
            }
        }
        //清空
        protected void ClearListBox(string name)
        {
            Control c = GetControl(name);
            if (c != null && c.InvokeRequired)
            {
                c.BeginInvoke(new MyDelegate1(ClearListBox), name);
            }
            else if (c != null)
            {
                ((ListBox)c).Items.Clear();
            }
        }
        //核对消息框
        protected bool ShowCheckDialog(string mess)
        {
            if (MessageBox.Show(mess, "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        //提示消息框
        protected void ShowMessageDialog(string mess)
        {
            MessageBox.Show(mess, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        //重绘listitem
        protected void listBox_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index == -1)
            {
                return;
            }
            ListBox lb = sender as ListBox;
            Rectangle bound = e.Bounds;//获取当前要绘制的行的一个矩形范围。
            string text = lb.Items[e.Index].ToString();//获取当前要绘制的行的显示文本。
            Rectangle rect = new Rectangle(bound.Left, bound.Top, bound.Width, bound.Height);
            //绘制背景色
            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
            {
                e.Graphics.FillRectangle(Brushes.LightGray, rect);
            }
            else
            {
                e.DrawBackground();
            }
            //输出字体
            if (System.Text.RegularExpressions.Regex.IsMatch(text, "不存在|失败|错误|结束|完成|限制|停止使用|被关闭|标题重复|敏感信息|不正确|禁止发布|过于频繁|重复"))
            {
                e.Graphics.DrawString(text, e.Font, new SolidBrush(Color.Red), e.Bounds, StringFormat.GenericDefault);
            }
            else
            {
                e.Graphics.DrawString(text, e.Font, new SolidBrush(Color.Black), e.Bounds, StringFormat.GenericDefault);
            }
        }
        //生成listview 数据
        protected void CreateListView(ref SqlServerOperate sql, string listview, string cmd)
        {
            CreateListView(ref sql, listview, cmd, true);
        }
        protected void CreateListView(ref SqlServerOperate sql, string listview, string cmd, bool column)
        {
            List<ListViewItem> list = new List<ListViewItem>();
            SqlDataReader re = sql.Query(cmd);
            int len = re.FieldCount;
            if (column)
            {
                ColumnHeader[] ch = new ColumnHeader[len];
                for (int i = 0; i < len; i++)
                {
                    ColumnHeader _ch = new ColumnHeader();
                    _ch.Text = re.GetName(i);
                    ch[i] = _ch;
                }
                SetControl(listview, ControlValue.ListViewHeader, ch);
            }
            string[] str = new string[len];
            while (re.Read())
            {
                string[] k = new string[len];
                for (int i = 0; i < len; i++)
                {
                    k[i] = re[i].ToString();
                }
                list.Add(new ListViewItem(k));
            }
            re.Close();
            SetControl(listview, ControlValue.ListView, list);
        }
        //生成菜单
        protected void CreateListViewContentScriptMenu(object _lv)
        {
            try
            {
                ListView lv = _lv as ListView;
                if (lv != null && lv.InvokeRequired)
                {
                    lv.BeginInvoke(new MyDelegate1_2(CreateListViewContentScriptMenu), _lv);
                }
                else if (lv != null)
                {
                    ContextMenuStrip cms = new ContextMenuStrip();
                    for (int i = 0; i < lv.Columns.Count; i++)
                    {
                        string name = lv.Columns[i].Text;
                        switch (name)
                        {
                            case "网址":
                                {
                                    ToolStripItem ts = new ToolStripMenuItem("复制网址");
                                    ts.Tag = i;
                                    ts.Click += delegate
                                    {
                                        if (lv.SelectedItems.Count > 0)
                                        {
                                            Clipboard.SetDataObject(lv.SelectedItems[0].SubItems[Convert.ToInt32(ts.Tag)].Text);
                                        }
                                    };
                                    cms.Items.Add(ts);
                                    //如果是网址
                                    if (name.Equals("网址"))
                                    {
                                        ts = new ToolStripMenuItem("打开网址");
                                        ts.Tag = i;
                                        ts.Click += delegate
                                        {
                                            if (lv.SelectedItems.Count > 0)
                                            {
                                                RunProgram(lv.SelectedItems[0].SubItems[Convert.ToInt32(ts.Tag)].Text);
                                            }
                                        };
                                        cms.Items.Add(ts);
                                    }
                                    break;
                                }
                            case "关键字":
                                {
                                    //如果是关键字
                                    if (name.Equals("关键字"))
                                    {
                                        ToolStripItem ts = new ToolStripMenuItem("查看关键字百度结果");
                                        ts.Tag = i;
                                        ts.Click += delegate
                                        {
                                            if (lv.SelectedItems.Count > 0)
                                            {
                                                RunProgram("http://www.baidu.com/s?wd=" + lv.SelectedItems[0].SubItems[Convert.ToInt32(ts.Tag)].Text);
                                            }
                                        };
                                        cms.Items.Add(ts);
                                    }
                                    break;
                                }
                        }
                    }
                    lv.ContextMenuStrip = cms;
                }
            }
            catch (Exception e)
            {
                ShowMessageDialog("程序错误:" + e.Message);
            }
        }
        //是否滚屏
        protected void Scrolling(bool b)
        {
            this.scroll = b;
        }
        //运行程序
        private void RunProgram(string path)
        {
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.UseShellExecute = true;
            p.StartInfo.FileName = path;
            p.Start();
        }
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(BaseForm));
            this.SuspendLayout();
            // 
            // BaseForm
            // 
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "BaseForm";
            this.ResumeLayout(false);

        }
        //弹出窗口
        protected void ShowForm(Form f)
        {
            if (this.MdiParent != null)
            {
                f.MdiParent = this.MdiParent;
            }
            else
            {
                f.MdiParent = this;
            }
            f.Show();
        }
    }
    public enum ControlValue : int
    {
        Text,
        Name,
        Enable,
        Checked,
        DateTimePicker,
        ListBox,
        ListViewHeader,
        ListView,
        Combobox,
        PictureBox,
        StripComboBox,
        WebBrowser
    }
}